﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;


namespace LoD.Tools.TilesetMaker {
    /// <summary>
    /// Found on StackOverflow: http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum
    /// </summary>
    public class EnumBooleanConverter : IValueConverter {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var parameterString = parameter as string;
            if (parameterString == null) return DependencyProperty.UnsetValue;

            if (Enum.IsDefined(value.GetType(), value) == false) return DependencyProperty.UnsetValue;

            object parameterValue = Enum.Parse(value.GetType(), parameterString);

            return parameterValue.Equals(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            var parameterString = parameter as string;
            return parameterString == null || value.Equals(false)
                       ? DependencyProperty.UnsetValue
                       : Enum.Parse(targetType, parameterString);
        }

        #endregion
    }
}